package org.wmq.travelagency02.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wmq.travelagency02.common.ChatUserHelper;
import org.wmq.travelagency02.common.UserContext;
import org.wmq.travelagency02.mapper.ChatUserMapper;
import org.wmq.travelagency02.model.Entity.ChatUser;
import org.wmq.travelagency02.model.Entity.Guidebook;
import org.wmq.travelagency02.model.Entity.Traveller;
import org.wmq.travelagency02.service.*;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class ChatUserServiceImpl extends ServiceImpl<ChatUserMapper, ChatUser> implements ChatUserService {
    private final ChatUserMapper chatUserMapper;
    private final ChatUserHelper chatUserHelper;
    private final ContactService contactService;
    private final ContactGroupService contactGroupService;
    @Lazy
    @Autowired
    private TravellerService travellerService;
    @Lazy
    @Autowired
    private GuidebookService guidebookService;
    @Override
    @Transactional
    public boolean saveChatUser(ChatUser chatUser) {
        chatUser.setCreateTime(LocalDateTime.now());
        chatUser.setUpdateTime(LocalDateTime.now());
        int n = this.chatUserMapper.insert(chatUser);
        if (n == 0){
            return false;
        }
        boolean success = this.chatUserHelper.addChatUserId(chatUser.getId(), UserContext.getUserId(), UserContext.getUserType());
        if (!success){
            throw new RuntimeException("添加聊天用户ID失败");
        }
        return true;
    }

    @Override
    public ChatUser getByUserId(Long userId, Byte userType) {
        return this.chatUserHelper.getChatUser(userId, userType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeChatUserById(String id) {
        boolean success = this.removeById(id);
        if (!success){
            return false;
        }
        try {
            this.travellerService.lambdaUpdate().eq(Traveller::getChatUserId, id)
                    .set(Traveller::getChatUserId, null).update();
        } catch (Exception e) {
            throw new RuntimeException("删除ChatUser异常，没有级联删除Traveller中的chatUserId字段");
        }
        try {
            this.guidebookService.lambdaUpdate().eq(Guidebook::getChatUserId, id)
                    .set(Guidebook::getChatUserId, null).update();
        } catch (Exception e) {
            throw new RuntimeException("删除ChatUser异常，没有级联删除Guidebook中的chatUserId字段");
        }
        try {
            this.contactService.removeByMasterId(id);
        } catch(Exception e){
            throw new RuntimeException("删除ChatUser异常，没有级联删除Contact中的联系人");
        }
        try {
            this.contactGroupService.removeByMasterId(id);
        }catch (Exception e){
            throw new RuntimeException("删除ChatUser异常，没有级联删除ContactGroup");
        }
        return true;
    }
}
